<?php
class DataBase{
	
	private $link = null;	
	private static $db = null;	
	
	public function __construct(){
		$config = array();
		include 'database.config.php';
		$this->connect($config);
	}
	
	public static function  getInstance(){
		if(self::$db===null){
			self::$db = new DataBase();
		}
		return self::$db;
	}
	
	
	public function connect($config){		
		$this->link = mysql_connect($config['host'],$config['user'],$config['pwd'],$config['charset']);
		if(!$this->link){
			die('Could not connect: '.mysql_error());
		}		
		if(mysql_select_db($config['dbname'],$this->link) === false){
			die('Can not use '.$config['dbname'].': '.mysql_error());	
		}		
		mysql_query("set names {$config['charset']}",$this->link);
	}
	
	public function get_all($sql){
		$result = mysql_query($sql, $this->link);
		if($result===false){
			die('sql: '.$sql.' '.mysql_error());
		}
		$num = mysql_num_rows($result);
		$rows = array();
		for($i=0;$i<$num;$i++){
			$rows[] = mysql_fetch_array($result,MYSQL_ASSOC);	
		}		
		return $rows; //mysql_fetch_array($result,MYSQL_ASSOC);
	}
	
	public function get_one($sql){
		$result = mysql_query($sql, $this->link);
		if($result===false){
			die('sql: '.$sql.' '.mysql_error());
		}
		return mysql_fetch_array($result,MYSQL_ASSOC);
	}

	
	public function execute($sql){		
		$result = mysql_query($sql, $this->link);
		if($result===false){
			die('sql: '.$sql.' '.mysql_error());
		}		
	}
	
	public function __destruct(){
		$this->close();
	}
	
	public function close(){
		if($this->link){
			mysql_close($this->link);
		}
	}
	
}
?>